import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { validate } from '@/utils/validateUtil';
import { renderImage, renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public openid = ''; // 唯一用户ID
  public nickName = ''; // 用户昵称
  public avatarUrl = ''; // 头像地址
  public phone = ''; // 手机号
  public accessToken = ''; // access_token
  public createAt = ''; // 添加时间
  public updateAt = ''; // 更新时间
  public status = 1; // 状态

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  nickName: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入用户昵称',
  },
  phone: {
    required: false,
    trigger: ['blur', 'input'],
    type: 'string',
    validator: validate.phone,
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'nickName',
    component: 'NInput',
    label: '用户昵称',
    componentProps: {
      placeholder: '请输入用户昵称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'phone',
    component: 'NInput',
    label: '手机号',
    componentProps: {
      placeholder: '请输入手机号',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NRadioGroup',
    label: '状态',
    giProps: {
      //span: 24,
    },
    componentProps: {
      options: dict.getOption('sys_normal_disable'),
      onUpdateChecked: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: '唯一用户ID',
    key: 'openid',
    align: 'left',
    width: -1,
  },
  {
    title: '用户昵称',
    key: 'nickName',
    align: 'left',
    width: -1,
  },
  {
    title: '头像地址',
    key: 'avatarUrl',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderImage(row.avatarUrl);
    },
  },
  {
    title: '手机号',
    key: 'phone',
    align: 'left',
    width: -1,
  },
  {
    title: 'access_token',
    key: 'accessToken',
    align: 'left',
    width: -1,
  },
  {
    title: '添加时间',
    key: 'createAt',
    align: 'left',
    width: -1,
  },
  {
    title: '更新时间',
    key: 'updateAt',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_normal_disable', row.status);
    },
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_normal_disable']);
}